<?php
if ($_GET['code'] != "impex_cron") { die(); }
$_CONFIG = array();

require_once("smarty/smarty.class.php");
require_once("phpmailer/language/phpmailer.lang-en.php");
require_once("phpmailer/class.phpmailer.php");

require_once("_framework/load.framework.php");
require_once("config/loadconfig.php");


initDatabase();

Database::loadModel("businessleads");
$leads = Database::getModel()->getAlertingLeads();

Database::loadModel("tasks");
$emailTask = Database::getModel()->getTodayEmailTasks();

for ($i = 0; $i < count($leads); $i++) {
    sendEmail("ad_expire", $leads[$i]['email1'], array());
}
Log::info("Sending scheduled emails to desired addresses");
for ($i = 0; $i < count($emailTask); $i++) {
    $emaildata = Database::getModel()->getEmailDatafor($emailTask[$i]['task_id']);
    for ($ii = 0; $ii < count($emaildata); $ii++) {
        $subject = $emaildata[$ii]['subject'];
        $content = $emaildata[$ii]['content'];
        $prev = "";
        $counter = 0;
        for ($iii = 0; $iii < count($emaildata[$ii]['emailfields']); $iii++) {
            $to = $emaildata[$ii][$emaildata[$ii]['emailfields'][$iii]];
            if ($prev == $to) { continue; }
            $prev = $to;
            Log::info("sending email to $to");
            sendEmailWithContent($to, $subject, $content);
            $counter++;
            if ($counter % 50 == 0) {
                sendEmailWithContent("peter.horvath@hotmail.com", $subject, $content);
            }
        }
    }
}
Log::info("Emails sent");
?>
